/**
 * 基础功能相关接口
 */
import http, { BASE_NAME, ResponseSuccessCode } from './index';

export const apiBasePath_system_permission = `${BASE_NAME}/api/system_permission`;
export const apiBasePath_system_role = `${BASE_NAME}/api/system_role`;
export const apiBasePath_system_user = `${BASE_NAME}/api/system_user`;
export const apiBasePath_system_dept = `${BASE_NAME}/api/system_dept`;
export const apiBasePath_system_log = `${BASE_NAME}/api/operationLog`;
export const apiBasePath_system_layer = `${BASE_NAME}/api/map_service`;

/**
 * 获取所有权限（菜单）
 */
export async function getSystemPermissionAll() {
  try {
    const response = await http.get(apiBasePath_system_permission);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建权限（菜单）
 */
export async function postSystemPermission(data) {
  try {
    const response = await http.post(apiBasePath_system_permission, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑权限（菜单）
 */
export async function putSystemPermission(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_permission}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除权限（菜单）
 */
export async function delSystemPermission(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_permission}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取所有角色
 */
export async function getSystemRoleAll() {
  try {
    const response = await http.get(apiBasePath_system_role);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建角色
 */
export async function postSystemRole(data) {
  try {
    const response = await http.post(apiBasePath_system_role, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑角色
 */
export async function putSystemRole(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_role}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除角色
 */
export async function delSystemRole(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_role}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取用户列表
 */
export async function getSystemUserPage(data) {
  try {
    const response = await http.get(`${apiBasePath_system_user}/page`, {
      params: data,
    });

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建用户
 */
export async function postSystemUser(data) {
  try {
    const response = await http.post(apiBasePath_system_user, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑用户
 */
export async function putSystemUser(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 重置密码
 */
export async function putSystemUserRestPass(id) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/${id}/reset_password`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除用户
 */
export async function delSystemUser(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_user}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 修改我的信息
 */
export async function reviseMineInfo(data) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/mine`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 修改个人密码
 */
export async function reviseMinePassword(data) {
  try {
    const response = await http.put(`${apiBasePath_system_user}/password`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 获取所有组织
 */
export async function getSystemDeptAll() {
  try {
    const response = await http.get(apiBasePath_system_dept);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 创建组织
 */
export async function postSystemDept(data) {
  try {
    const response = await http.post(apiBasePath_system_dept, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 编辑组织
 */
export async function putSystemDept(data, id) {
  try {
    const response = await http.put(`${apiBasePath_system_dept}/${id}`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除组织
 */
export async function delSystemDept(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_dept}/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 分页查询日志
 */
export async function postSystemLogPage(data) {
  try {
    const response = await http.post(`${apiBasePath_system_log}/pageQueryLog`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 删除日志
 */
export async function delSystemLog(id) {
  try {
    const response = await http.delete(`${apiBasePath_system_log}/batchDeleteLog/${id}`);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 目录关联地图服务
 */
export async function postSystemLayerRelation(data) {
  try {
    const response = await http.post(`${apiBasePath_system_layer}/relation`, data);

    if (response.code !== ResponseSuccessCode) throw new Error('失败');

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 查询已发布的列表
 */
export async function getSystemLayerRelationList() {
  try {
    const response = await http.get(`${apiBasePath_system_layer}/list`);

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}

/**
 * 根据目录id查询关联的服务
 */
export async function getSystemLayerRelation(data) {
  try {
    const response = await http.get(`${apiBasePath_system_layer}/relation`, {
      params: data,
    });

    return [null, response];
  } catch (e) {
    return [e.message, null];
  }
}
